from os import path, makedirs
from PIL.DdsImagePlugin import Image

def CropDds():
    try:
        # 用户文件夹.
        strUserDir = path.join("D:", path.sep, "Program", "CdiuMod", "User")
        # 保存文件夹.
        strSaveDir = path.join(strUserDir, "OBSImg")
        strDdsFile, strSaveExt = "bg.dds", ".png"
        
        # /* 确保保存文件夹存在. */
        if not path.exists(strSaveDir):
            makedirs(strSaveDir)
        
        # /* 定义裁剪区域. */
        dicPngFile = {
            "tl": (0, 0, 240, 110),     # 240 x 110
            "bt": (0, 740, 1014, 768),  # 1014 x 28
            "rt": (1014, 0, 1024, 768), # 10 x 768
        }
        
        # /* 打开舞台文件. */
        with Image.open(path.join(strUserDir, strDdsFile)) as imgDds:
            for strName, tupRect in dicPngFile.items():
                rectCrop = imgDds.crop(tupRect)
                rectCrop.save(path.join(strSaveDir, strName + strSaveExt))
                rectCrop.close()
        
        print("裁剪成功!")
    except Exception as ex:
        print(ex.args)

if __name__ == "__main__":
    CropDds()